home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / FZNUM.ZIP / fuzzy / confint / confint.cc next >
Encoding:
C/C++ Source or Header  |  1994-10-03  |  8.0 KB  |  364 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. // CONFINT.CC
  4. //
  5. //    Confidence interval type implementation.
  6. //
  7. //    Based on A. Kaufmann, M. Gupta, "Introduction to Fuzzy Arithmetic",
  8. //    Van Nostrand Reinhold, New York, 1991.
  9. //
  10. // Contents:
  11. //
  12. //    double min4( double opnd1, double opnd2, double opnd3, double opnd4)
  13. //    double max4( double opnd1, double opnd2, double opnd3, double opnd4)
  14. //
  15. //    ConfInt::ConfInt( double lBound, double uBound)
  16. //
  17. //    ConfInt ConfInt::operator*( ConfInt opnd2)
  18. //    ConfInt ConfInt::operator*=( ConfInt opnd2)
  19. //
  20. //    ConfInt operator/( double opnd1, ConfInt opnd2)
  21. //
  22. //    ConfInt sin( ConfInt opnd)
  23. //    ConfInt cos( ConfInt opnd)
  24. //
  25. // Author: S. Deodato ( 02.03.94)
  26. //
  27. //---------------------------------------------------------------------------
  28.  
  29. #include "confint.h"
  30.  
  31. #include <stdlib.h>
  32.  
  33.  
  34. //---------------------------------------------------------------------------
  35. //
  36. // double min4( double opnd1, double opnd2, double opnd3, double opnd4)
  37. //
  38. //    Calculate the minimum from 4 double values.
  39. //
  40. // Arguments:
  41. //
  42. //    double opnd1
  43. //    double opnd2
  44. //    double opnd3
  45. //    double opnd4
  46. //       the input values
  47. //
  48. // Return value:
  49. //
  50. //    the computed minimum
  51. //
  52. // Side effects:
  53. //
  54. //    none
  55. //
  56. // Author: S. Deodato ( 05.03.94)
  57. //
  58. //---------------------------------------------------------------------------
  59.  
  60. double min4( double opnd1, double opnd2, double opnd3, double opnd4)
  61.  
  62. {
  63.    double min = ( opnd1 < opnd2) ? opnd1 : opnd2;
  64.  
  65.    if ( opnd3 < min) min = opnd3;
  66.    if ( opnd4 < min) min = opnd4;
  67.  
  68.    return min;
  69. }
  70.  
  71.  
  72. //---------------------------------------------------------------------------
  73. //
  74. // double max4( double opnd1, double opnd2, double opnd3, double opnd4)
  75. //
  76. //    Calculate the maximum from 4 double values.
  77. //
  78. // Arguments:
  79. //
  80. //    double opnd1
  81. //    double opnd2
  82. //    double opnd3
  83. //    double opnd4
  84. //       the input values
  85. //
  86. // Return value:
  87. //
  88. //    the computed maximum
  89. //
  90. // Side effects:
  91. //
  92. //    none
  93. //
  94. // Author: S. Deodato ( 05.03.94)
  95. //
  96. //---------------------------------------------------------------------------
  97.  
  98. double max4( double opnd1, double opnd2, double opnd3, double opnd4)
  99.  
  100. {
  101.    double max = ( opnd1 > opnd2) ? opnd1 : opnd2;
  102.  
  103.    if ( opnd3 > max) max = opnd3;
  104.    if ( opnd4 > max) max = opnd4;
  105.  
  106.    return max;
  107. }
  108.  
  109.  
  110. //---------------------------------------------------------------------------
  111. //
  112. // ConfInt::ConfInt( double lBound, double uBound)
  113. //
  114. //    Confidence interval constructor.
  115. //
  116. //    Confidence intervals require upper bound greater than (or equal to)
  117. //    lower bound.
  118. //
  119. // Arguments:
  120. //
  121. //    double lBound
  122. //    double uBound
  123. //       lower and upper bound for the confidence interval
  124. //
  125. // Return value:
  126. //
  127. //    a new confidence interval
  128. //
  129. // Side effects:
  130. //
  131. //    none
  132. //
  133. // Author: S. Deodato ( 26.04.94)
  134. //
  135. //---------------------------------------------------------------------------
  136.  
  137. ConfInt::ConfInt( double lBound, double uBound)
  138.  
  139.    :  lower( lBound),
  140.       upper( uBound)
  141.  
  142. {
  143.    if ( lBound > uBound) {
  144.  
  145.       cerr << "\n\nConfidence Interval Error:"
  146.            << "\n\nLower bound greater than upper bound in\n\n\t[ "
  147.            << lBound << " ," << uBound << "].\n";
  148.  
  149.       abort();
  150.    }
  151. }
  152.  
  153.  
  154. //---------------------------------------------------------------------------
  155. //
  156. // ConfInt ConfInt::operator*( ConfInt opnd2)
  157. //
  158. //    Multiplication between two confidence intervals.
  159. //
  160. // Arguments:
  161. //
  162. //    ConfInt opnd2
  163. //       the confidence interval to multiply for
  164. //
  165. // Return value:
  166. //
  167. //    the resulting confidence value
  168. //
  169. // Side effects:
  170. //
  171. //    none
  172. //
  173. // Author: S. Deodato ( 02.03.94)
  174. //
  175. //---------------------------------------------------------------------------
  176.  
  177. ConfInt ConfInt::operator*( ConfInt opnd2)
  178.  
  179. {
  180.    double tmp1 = lower * opnd2.lower;
  181.    double tmp2 = lower * opnd2.upper;
  182.    double tmp3 = upper * opnd2.lower;
  183.    double tmp4 = upper * opnd2.upper;
  184.  
  185.    return ConfInt( min4( tmp1, tmp2, tmp3, tmp4),
  186.                    max4( tmp1, tmp2, tmp3, tmp4));
  187. }
  188.  
  189.  
  190. //---------------------------------------------------------------------------
  191. //
  192. // ConfInt ConfInt::operator*=( ConfInt opnd2)
  193. //
  194. //    Multiplication and assignment between two confidence intervals.
  195. //
  196. // Arguments:
  197. //
  198. //    ConfInt opnd2
  199. //       the confidence interval to multiply for
  200. //
  201. // Return value:
  202. //
  203. //    the resulting confidence value
  204. //
  205. // Side effects:
  206. //
  207. //    updates the current confidence interval
  208. //
  209. // Author: S. Deodato ( 02.03.94)
  210. //
  211. //---------------------------------------------------------------------------
  212.  
  213. ConfInt ConfInt::operator*=( ConfInt opnd2)
  214.  
  215. {
  216.    double tmp1 = lower * opnd2.lower;
  217.    double tmp2 = lower * opnd2.upper;
  218.    double tmp3 = upper * opnd2.lower;
  219.    double tmp4 = upper * opnd2.upper;
  220.  
  221.    lower = min4( tmp1, tmp2, tmp3, tmp4);
  222.    upper = max4( tmp1, tmp2, tmp3, tmp4);
  223.  
  224.    return *this;
  225. }
  226.  
  227.  
  228. //---------------------------------------------------------------------------
  229. //
  230. // ConfInt operator/( double opnd1, ConfInt opnd2)
  231. //
  232. //    Division of a singleton by a confidence interval.
  233. //
  234. // Arguments:
  235. //
  236. //    double opnd1
  237. //    ConfInt opnd2
  238. //       division operands
  239. //
  240. // Return value:
  241. //
  242. //    the resulting confidence value
  243. //
  244. // Side effects:
  245. //
  246. //    none
  247. //
  248. // Author: S. Deodato ( 02.03.94)
  249. //
  250. //---------------------------------------------------------------------------
  251.  
  252. ConfInt operator/( double opnd1, ConfInt opnd2)
  253.  
  254. {
  255.    if ( opnd2.lower * opnd2.upper <= 0) {
  256.  
  257.       cerr << "\n\nConfidence Interval Error:"
  258.            << "\n\nDivision by zero error in\n\n\t"
  259.            << opnd1 << "/" << opnd2 << ".\n";
  260.  
  261.       abort();
  262.    }
  263.  
  264.    return opnd1 * ConfInt( 1 / opnd2.upper, 1 / opnd2.lower);
  265. }
  266.  
  267.  
  268. //---------------------------------------------------------------------------
  269. //
  270. // ConfInt sin( ConfInt opnd)
  271. //
  272. //    Calculate sine of confidence intervals.
  273. //
  274. // Arguments:
  275. //
  276. //    ConfInt opnd
  277. //       a confidence interval (expressed in radiants, not degree)
  278. //
  279. // Return value:
  280. //
  281. //    the resulting confidence interval
  282. //
  283. // Side effects:
  284. //
  285. //    none
  286. //
  287. // Author: S. Deodato ( 26.04.94)
  288. //
  289. //---------------------------------------------------------------------------
  290.  
  291. ConfInt sin( ConfInt opnd)
  292.  
  293. {
  294.    if ( opnd.upper - opnd.lower >= ( 2 * M_PI))
  295.       return ConfInt( -1, 1);
  296.  
  297.    int offset = int( opnd.lower / ( 2 * M_PI));
  298.    double max = ( M_PI / 2) + offset * ( 2 * M_PI);
  299.    double min = ( 3 * M_PI / 2) + offset * ( 2 * M_PI);
  300.    double low = sin( opnd.lower);
  301.    double upp = sin( opnd.upper);
  302.  
  303.    double lower = (( opnd.lower <= min && opnd.upper >= min)
  304.            || opnd.upper >= ( min + ( 2 * M_PI)))
  305.                 ? -1
  306.                 : ( low < upp) ? low : upp;
  307.  
  308.    double upper = (( opnd.lower <= max && opnd.upper >= max)
  309.                    || opnd.upper >= ( max + ( 2 * M_PI)))
  310.                 ? 1
  311.                 : ( low > upp) ? low : upp;   
  312.  
  313.    return ConfInt( lower, upper);
  314. }
  315.  
  316.  
  317. //---------------------------------------------------------------------------
  318. //
  319. // ConfInt cos( ConfInt opnd)
  320. //
  321. //    Calculate cosine of confidence intervals.
  322. //
  323. // Arguments:
  324. //
  325. //    ConfInt opnd
  326. //       a confidence interval (expressed in radiants, not degree)
  327. //
  328. // Return value:
  329. //
  330. //    the resulting confidence interval
  331. //
  332. // Side effects:
  333. //
  334. //    none
  335. //
  336. // Author: S. Deodato ( 26.04.94)
  337. //
  338. //---------------------------------------------------------------------------
  339.  
  340. ConfInt cos( ConfInt opnd)
  341.  
  342. {
  343.    if ( opnd.upper - opnd.lower >= ( 2 * M_PI))
  344.       return ConfInt( -1, 1);
  345.  
  346.    int offset = int( opnd.lower / ( 2 * M_PI));
  347.    double max = offset * ( 2 * M_PI);
  348.    double min = M_PI + offset * ( 2 * M_PI);
  349.    double low = cos( opnd.lower);
  350.    double upp = cos( opnd.upper);
  351.  
  352.    double lower = (( opnd.lower <= min && opnd.upper >= min)
  353.                    || opnd.upper >= ( min + ( 2 * M_PI)))
  354.                 ? -1
  355.                 : ( low < upp) ? low : upp;
  356.  
  357.    double upper = (( opnd.lower <= max && opnd.upper >= max)
  358.                    || opnd.upper >= ( max + ( 2 * M_PI)))
  359.                 ? 1
  360.                 : ( low > upp) ? low : upp;   
  361.  
  362.    return ConfInt( lower, upper);
  363. }
  364.